package com.py.credit.utils;

import com.py.credit.entity.TreeObj;

import java.util.*;

public abstract class TreeUtil {

    public abstract void newTreeObject(List<TreeObj> dataList, List<TreeObj> srcList, Map<String, TreeObj> cleanMap);

    public List<TreeObj> getTree(List<TreeObj> dataList) {
        List<TreeObj> srcList = new ArrayList<TreeObj>();
        Map<String, TreeObj> cleanMap = new HashMap<String, TreeObj>();
        newTreeObject(dataList, srcList, cleanMap);
        // 过滤无法关联的菜单，垃圾数据
        List<TreeObj> cleanList = new ArrayList<TreeObj>();
        for (TreeObj vi : srcList) {
            if (isRelax(cleanMap, vi.getId())) {
                cleanList.add(vi);
            }
        }
        // 构造树结构
        List<TreeObj> resultList = getResult(cleanList);

        return resultList;
    }

    private boolean isRelax(Map<String, TreeObj> cleanMap, String id) {
        if (cleanMap.containsKey(id)) {
            TreeObj menu = cleanMap.get(id);
            if ("0".equals(menu.getpId()) || null == menu.getpId()) {
                return true;
            } else {
                return isRelax(cleanMap, menu.getpId());
            }
        } else {
            return false;
        }
    }

    private List<TreeObj> getResult(List<TreeObj> src) {
        List<TreeObj> parents = new ArrayList<TreeObj>();
        TreeObj result = null;
        for (TreeObj ent : src) {
            if ("0".equals(ent.getpId()) || null == ent.getpId()) {
                result = ent;
                result.setChildren(new ArrayList<TreeObj>());
                parents.add(result);
            }
        }
        List<TreeObj> last = new ArrayList<TreeObj>();
        for (TreeObj ent : src) {
            if (!"0".equals(ent.getpId()) && null != ent.getpId()) {
                last.add(ent);
            }
        }
//		Collections.sort(parents);
//		Collections.sort(last);
        buildTree(parents, last);
        return parents;
    }

    private void buildTree(List<TreeObj> parents, List<TreeObj> others) {
        List<TreeObj> record = new ArrayList<TreeObj>();
        TreeObj vi = null;
        TreeObj inVi = null;
        for (Iterator<TreeObj> it = parents.iterator(); it.hasNext(); ) {
            vi = it.next();
            if (vi.getId().equals(null) || !vi.getId().equals("0")) {
                for (Iterator<TreeObj> otherIt = others.iterator(); otherIt.hasNext(); ) {
                    inVi = otherIt.next();
                    if (vi.getId().equals(inVi.getpId())) {
                        if (null == vi.getChildren()) {
                            vi.setChildren(new ArrayList<TreeObj>());
                        }
                        vi.getChildren().add(inVi);
                        record.add(inVi);
                        otherIt.remove();
                    }
                }
            }
        }
        if (others.size() == 0) {
            return;
        } else {
            buildTree(record, others);
        }
    }
}
